; progman.asm
; GUI study for a program manager

	.include "common.inc"
	.include "global.inc"
	.include "window.inc"
	.include "screen.inc"
	.include "wutil.inc"
	.include "execfun.inc"
	
	.macpack cbm

	.export menu_init

	.import ctg_init
	.import ctg_run 

	INITX = 8
	INITY = 16
	SIZEX = 16
	SIZEY = 16

	.segment "RODATA"
	
drive8:		.byte $AA, $55, $7F, $80, $81, $9F, $81, $80
			.byte $7F, $50, $A9, $52, $A9, $52, $A9, $50
			.byte $AA, $55, $FE, $01, $81, $F9, $81, $01
			.byte $FE, $15, $8A, $55, $8A, $55, $8A, $15
bm_drive8:	.byte 16, 16
			.word drive8
tx_drive8:	.byte $FF
			.word bm_drive8
	
tx_title:	.byte 8 
			scrcode "Programs"

b_chicken:	.byte %00111110, %01111110, %11100001, %11100001
			.byte %11100000, %11100001, %11100001, %01111110
			.byte %00111110, %00000000, %00000000, %11100000
			.byte %11000000, %00000000, %11000000, %11100000
			.byte %00000000, %00000000

bm_chicken:	.byte 11, 9
			.word b_chicken

tx_start:	.byte $FF
			.word bm_chicken
			
tx_prg:		.byte 7
			scrcode "ctg.bas"

tx_bas0:	.byte 11
			scrcode "10 REM DEMO"
tx_bas1:	.byte 13
			scrcode "20 PRINT"
			.byte 34
			scrcode "CTG"
			.byte 34
tx_bas2:	.byte 10
			scrcode "30 GOTO 20"	

tx_run:		.byte 3
			scrcode "RUN"

	.segment "DATA"
	
curfrm:		.byte 0
curwin:		.byte 0

f_progman:	.byte 0
h_progman:	.byte 0

f_basic:	.byte 0
h_basic:	.byte 0
	
	.segment "CODE"

menu_init:	jsr ctg_init
			; create drive icon
			ldx #SIZEX
			ldy #SIZEY
			jsr frm_create
			sta curfrm
			ldx #INITX
			ldy #INITY
			jsr frm_setpos
			ldy #FFLG_PERM
			jsr frm_setflags
			jsr frm_getwin
			sta curwin
			jsr win_realize
			lda #WSTYLE_TXLEFT|WSTYLE_BCKGND|WSTYLE_TXTOP
			jsr win_setstyle
			ldx #>tx_drive8
			lda #<tx_drive8
			jsr win_settext
			lda #WTYPE_BUTTON
			jsr win_settype
			lda #<ex_drive_button
			ldx #>ex_drive_button
			jsr win_setexec

			; draw window (icon)			
			lda curwin
			jsr win_draw
			lda curwin
			jsr win_show			; to screen

			rts

;
;	Icon button exec function
;

ex_drive_button:
			jsr btncheck
			; toggle program manager window
			lda f_progman
			beq progman
			jmp frm_activate
			rts

;
;	Program manager window
;

progman:	ldx #MAX_WIDTH/2
			ldy #MAX_HEIGHT/5
			jsr frm_create
			sta f_progman
			ldx #MAX_WIDTH/8
			ldy #MAX_HEIGHT/6+16
			jsr frm_setpos
			jsr frm_getwin
			sta h_progman
			jsr win_realize
			lda #WSTYLE_BCKGND+WSTYLE_HASTITLE
			jsr win_setstyle
			lda #1					; lower case system font
			jsr win_setfont
			lda #<tx_title
			ldx #>tx_title
			jsr win_settext
			lda #<ex_progman		; add close button & handler
			ldx #>ex_progman
			jsr win_setexec
			lda h_progman
			jsr addclsbtn
			
			; Chicken log button
			lda h_progman
			jsr win_create
			lda #0
			jsr win_setstyle
			ldx #8
			ldy #20
			jsr win_setpos
			ldx #30
			ldy #13
			jsr win_setdim
			lda #<tx_start
			ldx #>tx_start
			jsr win_settext
			lda #WTYPE_BUTTON
			jsr win_settype
			lda #<ex_start_button
			ldx #>ex_start_button
			jsr win_setexec
			
			; sub title
			lda h_progman
			jsr win_create
			lda #0
			jsr win_setstyle
			ldx #8
			ldy #36
			jsr win_setpos
			ldx #30
			ldy #12
			jsr win_setdim
			lda #<tx_prg
			ldx #>tx_prg
			jsr win_settext
			lda #2					; small font
			jsr win_setfont

			; draw window			
			lda h_progman
			jsr win_draw
			lda h_progman
			jsr win_show			; to screen

			rts

;
;	Event handler for program window
;

ex_progman:
			ldy #Event::type
			lda (reg2), y
			cmp #EVT_WIN_DESTROY
			beq @close
			jmp ex_window
@close:		lda #0
			sta f_progman
			rts

;
;	Icon button exec function
;

ex_start_button:
			jsr btncheck
			; toggle program listing window
			lda f_basic
			beq basic_show
			jmp frm_activate
			rts

;
;	Basic text window
;

basic_show:
			ldx #MAX_WIDTH/2
			ldy #MAX_HEIGHT/3
			jsr frm_create
			sta f_basic
			ldx #MAX_WIDTH/4
			ldy #MAX_HEIGHT/3+16
			jsr frm_setpos
			jsr frm_getwin
			sta h_basic
			jsr win_realize
			lda #WSTYLE_BCKGND+WSTYLE_HASTITLE
			jsr win_setstyle
			lda #1					; lower case system font
			jsr win_setfont
			lda #<tx_prg
			ldx #>tx_prg
			jsr win_settext
			lda #<ex_basic		; add close button & handler
			ldx #>ex_basic
			jsr win_setexec
			lda h_basic
			jsr addclsbtn
			
			; create source frame 
			lda h_basic
			jsr win_create
			lda #WSTYLE_BORDER
			jsr win_setstyle
			ldx #8
			ldy #16
			jsr win_setpos
			ldx #88
			ldy #60
			jsr win_setdim
			
			; create basic text
			lda h_basic
			jsr win_create
			lda #WSTYLE_TXLEFT
			jsr win_setstyle
			ldx #12
			ldy #18
			jsr win_setpos
			ldx #80
			ldy #12
			jsr win_setdim
			lda #2					; small font
			jsr win_setfont
			lda #<tx_bas0
			ldx #>tx_bas0
			jsr win_settext
			lda h_basic
			jsr win_create
			lda #WSTYLE_TXLEFT
			jsr win_setstyle
			ldx #12
			ldy #32
			jsr win_setpos
			ldx #80
			ldy #12
			jsr win_setdim
			lda #2					; small font
			jsr win_setfont
			lda #<tx_bas1
			ldx #>tx_bas1
			jsr win_settext
			lda h_basic
			jsr win_create
			lda #WSTYLE_TXLEFT
			jsr win_setstyle
			ldx #12
			ldy #46
			jsr win_setpos
			ldx #80
			ldy #12
			jsr win_setdim
			lda #2					; small font
			jsr win_setfont
			lda #<tx_bas2
			ldx #>tx_bas2
			jsr win_settext

			; create "run" button
			lda h_basic
			jsr win_create
			lda #WSTYLE_BORDER
			jsr win_setstyle
			ldx #30
			ldy #80
			jsr win_setpos
			ldx #44
			ldy #12
			jsr win_setdim
			lda #<tx_run
			ldx #>tx_run
			jsr win_settext
			lda #WTYPE_BUTTON
			jsr win_settype
			lda #<ex_run_button
			ldx #>ex_run_button
			jsr win_setexec

			; draw window			
			lda h_basic
			jsr win_draw
			lda h_basic
			jsr win_show			; to screen

;
;	Event handler for Basic window
;

ex_basic:
			ldy #Event::type
			lda (reg2), y
			cmp #EVT_WIN_DESTROY
			beq @close
			jmp ex_window
@close:		lda #0
			sta f_basic
			rts

;
;	RUN button exec function
;

ex_run_button:
			jsr btncheck
			; RUN program
			jmp ctg_run

